package com.gugu.procedure.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapUtil {


    /**
     * 返回两个Map中值不一样的字段
     *
     * @param originalMap
     * @param newMap
     * @return
     */
    public static List<Map<String, Item>> differenceCols(Map<String, String> originalMap, Map<String, String> newMap) {
        List<Map<String, Item>> list = new ArrayList<>();
        if (originalMap == null || newMap == null) {
            return list;
        }
        for (Map.Entry<String, String> entry : originalMap.entrySet()) {
            String key = entry.getKey();
            String originalValue = entry.getValue();
            String newValue = newMap.get(key);
            if (!newValue.equals(originalValue)) {
                Map<String, Item> map = new HashMap<>();
                Item item = new Item(originalValue, newValue);
                map.put(key, item);
                list.add(map);
            }
        }
        return list;
    }


    public static Long getLongValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value != null) {
            return Long.parseLong(value.toString());
        }
        return null;
    }

    public static Integer getIntegerValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value != null) {
            return Integer.parseInt(value.toString());
        }
        return null;
    }

    public static String getStringValue(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value != null) {
            return value.toString();
        }
        return null;
    }

    //Bean转换Map
    public static Map<String, Object> bean2MapObject(Object object) {
        if (object == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到属性对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(object);
                    map.put(key, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

    //Map转Bean
    public static Object map2Bean(Map map, Object object) {
        if (map == null || object == null) {
            return null;
        }
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 驼峰转下划线
                key = StringUtil.camelToUnderline(key);
                if (map.containsKey(key)) {
                    Object value = map.get(key);
//                    System.out.println("key:" + key + ",value:" + value);
                    // 得到属性对应的setter方法
                    Method setter = property.getWriteMethod();
                    setter.invoke(object, value);
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return object;
    }


}
